import { getLabelType, getLabelScenes, getLabelStatus, getCreateSourceType, getListSourceType, getCreateStatus } from './service';
import { message } from 'antd';

const formatData = data => {
  return data.map(item => {
    const key = Object.keys(item)[0];
    return {
      value: key,
      label: item[key].zh,
      label_en: item[key].en,
      disabled: item[key]?.disable,
    };
  })
};

export default {
  namespace: 'oldDataSet',

  state: {
    labelTypes: [],       // 标注类型
    labelScenes: [],      // 标注场景
    labelStatus: [],      // 标注状态
    createSourceType: [], // 数据来源（创建时）
    listSourceType: [],   // 数据来源（列表展示）
    createStatus: [],     // 创建状态
  },

  effects: {
    *getLabelType({ _ }, { call, put }) {
      const res = yield call(getLabelType);
      if (res.code === 0) {
        yield put({
          type: 'setState',
          payload: {
            labelTypes: formatData(res.data),
          }
        })
      } else {
        message.error('获取失败，请重试');
      }
    },
    *getLabelScenes({ _ }, { call, put }) {
      const res = yield call(getLabelScenes);
      if (res.code === 0) {
        yield put({
          type: 'setState',
          payload: {
            labelScenes: formatData(res.data),
          }
        })
      } else {
        message.error('获取失败，请重试');
      }
    },
    *getLabelStatus({ _ }, { call, put }) {
      const res = yield call(getLabelStatus);
      if (res.code === 0) {
        yield put({
          type: 'setState',
          payload: {
            labelStatus: formatData(res.data),
          }
        })
      } else {
        message.error('获取失败，请重试');
      }
    },
    *getCreateSourceType({ _ }, { call, put }) {
      const res = yield call(getCreateSourceType);
      if (res.code === 0) {
        yield put({
          type: 'setState',
          payload: {
            createSourceType: formatData(res.data),
          }
        })
      } else {
        message.error('获取失败，请重试');
      }
    },
    *getListSourceType({ _ }, { call, put }) {
      const res = yield call(getListSourceType);
      if (res.code === 0) {
        yield put({
          type: 'setState',
          payload: {
            listSourceType: formatData(res.data),
          }
        })
      } else {
        message.error('获取失败，请重试');
      }
    },
    *getCreateStatus({ _ }, { call, put }) {
      const res = yield call(getCreateStatus);
      if (res.code === 0) {
        yield put({
          type: 'setState',
          payload: {
            createStatus: formatData(res.data),
          }
        })
      } else {
        message.error('获取失败，请重试');
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },
  }
}